package com.zhou.JavaAndroidFirm.fragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.adapter.PhotoGirlAdapter;
import com.zhou.JavaAndroidFirm.base.BaseFragment;
import com.zhou.JavaAndroidFirm.bean.PhotoGirl;
import com.zhou.JavaAndroidFirm.constract.PhotosListContract;
import com.zhou.JavaAndroidFirm.model.PhotosListModel;
import com.zhou.JavaAndroidFirm.presenter.PhotosListPresenter;
import com.zhou.JavaAndroidFirm.widget.LoadingTip;
import com.zhou.JavaAndroidFirm.widget.NormalTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Administrator
 * @date 2018/7/15 0015
 * @des
 */
public class PhotosMainFragment extends BaseFragment<PhotosListModel, PhotosListPresenter> implements PhotosListContract.View, OnRefreshLoadMoreListener, View.OnClickListener {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.irc)
    RecyclerView irc;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    private List<PhotoGirl> mData = new ArrayList<>();
    private PhotoGirlAdapter mAdapter;

    private int size = 20;
    private int startPage = 1;

    @Override
    protected void lazyFetchData() {
        refresh.autoRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_photos_list;
    }

    @Override
    protected void initPresenter() {
        mPresenter.onCreate(mModel, this);
    }

    @Override
    protected void initView(Bundle bundle) {
        ntb.setTvLeftVisiable(false);
        ntb.setTitleText(getString(R.string.girl_title));
    }

    @Override
    protected void initEvent() {
        mAdapter = new PhotoGirlAdapter(mContext, R.layout.item_photo, mData);
        irc.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        irc.setAdapter(mAdapter);
        fab.setOnClickListener(this);
        refresh.setOnRefreshLoadMoreListener(this);
    }

    @Override
    public void returnPhotosListData(List<PhotoGirl> photoGirls) {
        mAdapter.addAll(photoGirls);
        setUi(refresh, loadedTip, mAdapter);
    }

    @Override
    public void showTip(String tip) {
        mAdapter.notifyDataSetChanged();
        setUi(refresh, loadedTip, mAdapter);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        startPage += 1;
        mPresenter.getPhotosListData(size, startPage);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mData.clear();
        startPage = 0;
        loadedTip.setVisibility(View.GONE);
        mPresenter.getPhotosListData(size, startPage);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fab:
                irc.smoothScrollToPosition(0);
                break;
            default:
                break;
        }
    }
}
